<?php
$urlManager = new CUrlManager();
$script = "
jQuery.fn.get_page_widget_id = function(obj)
{
    if (jQuery(obj).size())
        return parseInt(jQuery(obj).closest('li.sortable').attr('id').replace('id-', ''));
    return 0;
}
jQuery('#content').delegate('.page_delete', 'click', function(){
    if (!confirm('Are you sure you want to remove ?'))
        return false;
    var id = jQuery.fn.get_page_widget_id(this);
    jQuery.get('".$urlManager->createUrl('/Core/service/ajax', array('SID'=>'Cms.pageWidget.delete'))."', {'ids': [id]}, jQuery.proxy(function(res){
        res = eval(res);
        if (res.errors != undefined && res.errors.ErrorCode != undefined && res.errors.ErrorCode.length)
        {
            alert(res.errors.ErrorCode[0]);
        }
        else
        {
            jQuery(this).closest('li.sortable').remove();
            jQuery('#list-container').sortable('refresh');
        }
    },this));
    return false;
});
jQuery('#content').delegate('.sub_page', 'click', function(){
    var page_widget_id = jQuery.fn.get_page_widget_id(this);
    var url = jQuery.param.querystring(jQuery(this).attr('href'), {'page_widget_id' : page_widget_id});
    window.location = url;
    return false;
});
";
Yii::app()->clientScript->registerCoreScript('bbq');
Yii::app()->clientScript->registerScript(__CLASS__.'#RemoveWidget', $script, CClientScript::POS_READY);
?>
<div class="add-page">
    <?php echo CHtml::link('Add Widget', array('/Cms/admin/pageWidget/create', 'page_id'=>$page_id, 'revision_id'=>$revision_id, 'container'=>'cms-'.$type, 'layout'=>'popup'), array('title'=>'Add Widget', 'class'=>'sub_page button'));?>
</div>
<?php
$this->widget('zii.widgets.jui.CJuiSortable', array(
    'id'=>'list-container',
    'items'=>$items,
    'itemTemplate'=>'<li class="sortable" id="{id}"><div class="ie_fix sort-handle">{content}<div class="page_edit_delete"><a href="'.$urlManager->createUrl('/Cms/admin/widget/updateInstance', array('layout'=>'popup')).'" class="sub_page" title="Edit"><img width="23" height="29" alt="Edit Page" src="/themes/Admin/images/page/page_edit.png"></a>&nbsp;<a href="#" class="page_delete" title="Are you sure you want to remove?"><img width="23" height="29" alt="Remove Page" src="/themes/Admin/images/page/page_delete.png"></a></div>
</div></li>',
    'htmlOptions'=>array('class'=>'page_tree'),
    'options'=>array(
        'delay'=>'300',
        'update'=>'js:function(event, ui){jQuery.get("'.$urlManager->createUrl('/Core/service/ajax', array('SID'=>'Cms.pageWidget.order')).'", jQuery("#list-container").sortable("serialize"));}',
    ),
));

?>